﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Xml;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Collections.Specialized;
using MyToDoList.ViewModel;


namespace MyToDoList.Model
{
    public class Task 
    {

        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                if (value != _title)
                    _title = value;
                NotifyPropertyChanged("Title");
            }
        }

        private string _description;
        public string Description
        {
            get { return _description; }
            set
            {
                if (value != _description)
                    _description = value;
                NotifyPropertyChanged("Description");
            }
        }

        private bool _done;
        public bool Done
        {
            get { return _done; }
            set
            {
                if (value != _done)
                {
                    _done = value;
                    NotifyPropertyChanged("Done");
                }
            }
        }

        public bool IsChecked
        {
            get;
            set;
        }

        public Task()
        {
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public static void Save(ObservableCollection<Task> items)
        {
            // Write to the Isolated Storage
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Indent = true;

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Tasks.xml", FileMode.Create))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Task>));
                    using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                    {
                        serializer.Serialize(xmlWriter, items);
                    }
                }
            }
        }

        public static List<Task> LoadAll()
        {
            try
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    //myIsolatedStorage.DeleteFile("Tasks.xml");
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Tasks.xml", FileMode.OpenOrCreate))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(List<Task>));
                        List<Task> data = (List<Task>)serializer.Deserialize(stream);
                        return data;
                    }
                }
            }
            catch(Exception e)
            {
                return null;
            }
        }

       
    }

}
